package com.tan.androidtest.anim;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tan.androidtest.R;
import com.tan.androidtest.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * 测试LayoutAnimation,实现ListView的item的进入动画
 */

public class TestLayoutAnimationActivity extends BaseActivity {


    @Override
    public int initLayoutId() {
        return R.layout.activity_test_layout_animation_layout;
    }

    @Override
    public void initTitle(TextView tv_bar_left, TextView tv_bar_title, TextView tv_bar_right) {
        tv_bar_title.setText("测试LayoutAnimation设置ListViewItem进入动画");
    }

    @Override
    public void initView() {
        List<String> datas = new ArrayList<>();
        datas.add("11");
        datas.add("11");
        datas.add("11");
        datas.add("11");
        datas.add("11");
        datas.add("11");

        ListView lv_test = findViewById(R.id.lv_test);

        //方式2  在代码中设置
        Animation animation= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.right_to_left);
        LayoutAnimationController controller=new LayoutAnimationController(animation);
        controller.setDelay(0.3f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        lv_test.setLayoutAnimation(controller);

        MyAdapter myAdapter = new MyAdapter(datas);
        lv_test.setAdapter(myAdapter);




    }

    @Override
    public void initData() {

    }


    class MyAdapter extends BaseAdapter {

        List<String> datas = new ArrayList<>();


        MyAdapter(List<String> datas) {
            this.datas = datas;
        }

        @Override
        public int getCount() {
            return datas.size();
        }

        @Override
        public Object getItem(int position) {
            return datas.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            String content = (String) getItem(position);
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(TestLayoutAnimationActivity.this).inflate(R.layout.item_main_list_layout, parent, false);
                holder.tv_content = convertView.findViewById(R.id.tv_content);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.tv_content.setText(content);
            return convertView;
        }

        class Holder {
            TextView tv_content;
        }


    }
}
